package com.blog.oguzhan.soapui.utils.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;


public class OracleDatasource extends DBDatasource<Iterable<String>> {

	private Connection connection;
	
	{
		Class.forName( "oracle.jdbc.driver.OracleDriver" ).newInstance();
	}
	
	public OracleDatasource(String datasourceName, String username, String password, String host, String port, String databaseName, String sql) 
			throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
		
		super(datasourceName, username, password, host+":"+port+":"+databaseName);
		
		this.connection = DriverManager.getConnection("jdbc:oracle:thin:@" + getUrl(), username, password);

		super.setData(executeSQL(sql));
	}
	
	private Iterable<String> executeSQL(String sql) {
		LinkedList<String> data = new LinkedList<String>();
		try {
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				String value = resultSet.getString(1);
				data.add(value);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return data;
	}

}
